#include "base/logging.h"

#include "session_update_task.h"
#include "redis_session_adapter.h"
#include "shop_waiter_map.h"



namespace imserver {

void SessionUpdateTask::run() {
  std::vector<int64_t> shops = ShopWaiterMap::Instance().GetWaiterShops(id_);
  if (op_type_ == OP_ADD) {
    for(size_t i = 0; i < 3; ++i) {
      if (0 == redis_adapter_.AddSession(id_, session_)) {
        LOG_INFO("redis AddSession ok " << session_ << '/' << id_ << " retry=" << i);
        break;
      }
      LOG_WARN("redis AddSession err " << session_ << '/' << id_);
    }
    for(size_t j = 0; j < shops.size(); ++j) {
      for(size_t i = 0; i < 3; ++i) {
        if (0 == redis_adapter_.AddSession(shops[j], session_)) {
          LOG_INFO("redis shop AddSession ok " << session_ << '/' << shops[j] << " retry=" << i);
          break;
        }
        LOG_WARN("redis shop AddSession err " << session_ << '/' << shops[j]);
      }
    }
  } else if (op_type_ == OP_REMOVE) {
    for(int i = 0; i < 3; ++i) {
      if (0 == redis_adapter_.RemoveSession(id_, session_)) {
        LOG_INFO("redis RemoveSession ok " << session_ << '/' << id_ << " retry=" << i);
        break;
      }
      LOG_WARN("redis RemoveSession err " << session_ << '/' << id_);
    }

    for(size_t j = 0; j < shops.size(); ++j) {
      for(size_t i = 0; i < 3; ++i) {
        if (0 == redis_adapter_.RemoveSession(shops[j], session_)) {
          LOG_INFO("redis shop RemoveSession ok " << session_ << '/' << shops[j] << " retry=" << i);
          break;
        }
        LOG_WARN("redis shop RemoveSession err " << session_ << '/' << shops[j]);
      }
    }
  }
}

}


